<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Model\FishPond;
use App\Http\Model\Answer;
use App\Http\Model\Home_user;
use App\Http\Model\Type;

class FishPondController extends Controller
{
    public function answer($id)
    {
         
         $data = Answer::where('qid',$id)->get();

         //将查看回复列表中的uid替换为用户名
         foreach($data as $k=>$v){
            
            @$info = FishPond::find($v['id'])->user->username;
            
            if($data[$k]['uid'] == null){
               $data[$k]['uid'] = '管理员';
            }else{
                $data[$k]['uid'] = $info;
            }
         }
         
         // dd($data);
         return view('admin.fishpond.answer',['data'=>$data]);
    }

    public function reAnswer($id)
    {   

        $data = Answer::where('qid',$id)->get();
        // dd($data);
        return view('admin.fishpond.reAnswer',['data'=>$data]);
    }

    public function do_reAnswer(Request $request)
    {
        $data = $request->except('_token','username');
        // dd($data);

        $re = Answer::create($data);

        if($re){
            return redirect('admin/fishpond');
        }else{
            return back()->with('errors','添加失败');
        }
    }


    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        $data = FishPond::all();
        // dd($data);
        //将鱼塘问答中的tid替换为板块名
        foreach($data as $k=>$v){
            // dd($v);
            @$info = FishPond::find($v['id'])->type->name;
            
            $data[$k]['tid'] = $info;
        }

        //将鱼塘问答中的uid替换为提问者
        foreach($data as $k=>$v){
            @$info = FishPond::find($v['id'])->user->username;
            $data[$k]['uid'] = $info;
        }

        return view('admin.fishpond.index',compact('data'));
    }

    public function delAnswer($id)
    {
        $re = Answer::find($id)->delete();
        if($re){
            return redirect('admin/fishpond');
        }else{
            return back()->with('errors','添加失败');
        }
    }

    public function delQuestion($id)
    {
        
        $re = FishPond::find($id)->delete();

        $data = [];
        if($re){
            $data['status']= 0;
            $data['msg']='删除成功';
        }else{
            $data['status']= 1;
            $data['msg']='删除失败';
        }
        return $data;
    }


}
